package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.base.util.AjaxResult;
import cn.itsource.pethome.base.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController// @RestController是 一个组合注解     @RestController=@ResponseBody+@Controller
@RequestMapping("/department")
@Api(tags = "部门管理所有的接口",description = "部门接口详细描述")
public class DepartmentController {


    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IEmployeeService employeeService;

    //查询多级子部门
    @PatchMapping("/findDepartmentTree")
    public List<Department> findDepartmentTree(){
        return departmentService.findDepartmentTree();
    }

    //查询所有部门经理
    @PatchMapping("/findManagers")
    @ApiOperation(value = "查询所有的部门经理",notes = "")
    public List<Employee> findEmployee(){
        return employeeService.findEmployee();
    }


    @PatchMapping//  相当于  @RequestMapping(value = "/department",method = RequestMethod.PATCH)
    @ApiOperation(value = "查询所有的部门",notes = "我们这个接口有分页哦")
    public PageInfo<Department> findPageByQuery(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.findPageByQuery(departmentQuery);
    }


    //批量删除
    @PatchMapping("/batchRemove")//  相当于  @RequestMapping(value = "/department",method = RequestMethod.PATCH)
    @ApiOperation(value = "批量删除部门")
    public AjaxResult batchRemove(@RequestBody List<Department> departments){
        try {
            departmentService.batchRemove(departments);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }


    @GetMapping("/{id}")// 相当于      @RequestMapping(value = "/department/{id}",method = RequestMethod.GET)
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }


    //新增和修改都走此方法
    /*param department  如果id为空，就新增，如果id不为空就是修改
    @RequestBody :当你前端传递的参数是一个json对象的时候，并且后台想要接收对应的数
                 据，那后台必定要加此注解，否则接收不了前端传递的数据*/
    @PostMapping// 相当于    @RequestMapping(value = "/department",method = RequestMethod.POST)
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult save(@RequestBody Department department){
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }


    /*单个删除指定数据*/
    @DeleteMapping("/{id}")//相当于@RequestMapping(value = "/department/{id}",method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
}
